﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cua_Hang.DTO;
using System.Data;

namespace Cua_Hang.Admin
{
    public partial class QuanLy_ToanDo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Load_ToaDo_CuaHang_();
            }

            this.List_All_ToaDo();
            this.Bt_capnhat.Visible = false;
        }
        //==ham load all cua hang vao dropdowlist
        void Load_ToaDo_CuaHang_()
        {
            DDL_TenCuaHang.DataSource = Cua_Hang.BUS.Cua_Hang_BUS.ListCuaHangAll_BUS();
            DDL_TenCuaHang.DataTextField = "Ten_cuahang";
            DDL_TenCuaHang.DataValueField = "ID_cuahang";
            DDL_TenCuaHang.DataBind();

        }
        //---------------------------------------
        //==load all toa do cua hang vao gridview
        void List_All_ToaDo()
        {
            GV_ToaDo.DataSource = Cua_Hang.BUS.ToaDo_KhachHang_BUS.List_All_ToaDoKhachHang_BUS();
            GV_ToaDo.DataBind();
        }

        //ham xoa
        protected void GV_ToaDo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Int32 index = Int32.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "Xoa")
            {
                //String Code = GV_ChiTietCuaHang.DataKeys[index].Value.ToString();
                Cua_Hang.DTO.ToaDo_KhachHang_DTO chang = new DTO.ToaDo_KhachHang_DTO();
                chang.ID_ToaDo = Convert.ToInt32(e.CommandArgument);
                Cua_Hang.BUS.ToaDo_KhachHang_BUS.Xoa_ToaDo_CuaHang_BUS(chang);
                this.List_All_ToaDo();
            }
        }
        //---------------------------------------
        //====================ham kiem tra================================
        private bool KiemTra()
        {
            if (this.TB_ToaDoX.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập tọa độ x:!'); </script>");

                return false;
            }
            if (this.TB_ToaDoY.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập tọa độ y:!'); </script>");

                return false;
            }
            return true;

        }
        //----
        //----------them chi tiet cua hang------------------------------
        void Them_ToaDoCuaHang_GUI()
        {
            ToaDo_KhachHang_DTO ct = new ToaDo_KhachHang_DTO();
            ct.ID_cuahang = Convert.ToInt32(DDL_TenCuaHang.SelectedValue);
            ct.ToaDo_X = TB_ToaDoX.Text;
            ct.ToaDo_Y = TB_ToaDoY.Text;
            Cua_Hang.BUS.ToaDo_KhachHang_BUS.Them_ToaDo_CuaHang_BUS(ct);
        }

        //ham them--------------
        protected void Bt_them_Click(object sender, EventArgs e)
        {
            Cua_Hang.DTO.ToaDo_KhachHang_DTO chang = new DTO.ToaDo_KhachHang_DTO();
            chang.ToaDo_X = TB_ToaDoX.Text;
            chang.ToaDo_Y = TB_ToaDoY.Text;
            if (KiemTra() == true)
            {
                if (Cua_Hang.BUS.ToaDo_KhachHang_BUS.KiemTraTrung_ToaDo_CuaHang_BUS(chang) == false)
                {
                    this.Them_ToaDoCuaHang_GUI();
                    this.List_All_ToaDo();

                    TB_ToaDoX.Text = "";
                    TB_ToaDoY.Text = "";
                }
                else
                {
                    Response.Write(@"<script language = 'javascript'>alert ('Tọa độ  đã tồn tại !'); </script>");
                }
            }
        }
        //
        protected void GV_ToaDo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV_ToaDo.PageIndex = e.NewPageIndex;
            this.List_All_ToaDo();
        }
        //------------
        protected void GV_ToaDo_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }
        //-======
        protected void GV_ToaDo_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ID_ToaDo = Convert.ToInt32(GV_ToaDo.SelectedRow.Cells[0].Text);
            TB_ID_ToaDo.Text = ID_ToaDo.ToString();
            ToaDo_KhachHang_DTO dmn = new ToaDo_KhachHang_DTO();
            dmn = ShowChiTiet_ToaDo_KHTheoID(ID_ToaDo);
            TB_ToaDoX.Text = dmn.ToaDo_X.ToString();
            TB_ToaDoY.Text = dmn.ToaDo_Y.ToString();
            this.Bt_them.Visible = false;
            this.Bt_capnhat.Visible = true;
        }
        //=---=-=-=-=-=-=-=-=
        //============= chuyen List thanh DataTable===========================
        //
        public DataTable ConvertList_ToaDo_DTOToDataTable(List<ToaDo_KhachHang_DTO> lst)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("ID_ToaDo");
            dt.Columns.Add("ID_cuahang");
            dt.Columns.Add("ToaDo_X");
            dt.Columns.Add("ToaDo_Y");
            for (int i = 0; i < lst.Count; i++)
            {
                dt.Rows.Add(
                        lst[i].ID_ToaDo,
                        lst[i].ID_cuahang,
                        lst[i].ToaDo_X,
                        lst[i].ToaDo_Y

                    );
            }
            return dt;
        }

        //============= ket thuc chuyen List thanh DataTable=======================
        //==============ham hien vao textbox toa do theo id =================
        public ToaDo_KhachHang_DTO ShowChiTiet_ToaDo_KHTheoID(int ID_ToaDo)
        {
            ToaDo_KhachHang_DTO dm = new ToaDo_KhachHang_DTO();
            List<ToaDo_KhachHang_DTO> lst = BUS.ToaDo_KhachHang_BUS.List_ToaDoKhachHangTheoID_BUS(ID_ToaDo);
            DataTable dt = new DataTable();
            dt = ConvertList_ToaDo_DTOToDataTable(lst);
            foreach (DataRow dr in dt.Rows)
            {
                //dm.ID_cuahang = (int)dr["ID_cuahang"];
                //dm.ID_cuahang = (int)dr["ID_cuahang"];
                dm.ToaDo_X = dr["ToaDo_X"].ToString();
                dm.ToaDo_Y = dr["ToaDo_Y"].ToString();

            }
            return dm;
        }
        //---
        //----ham cap nhat toa do cua hang
        private void CapNhatToaDoCuaHang()
        {
            ToaDo_KhachHang_DTO dm = new ToaDo_KhachHang_DTO();
            //LayThongTinChiTietCuaHang(ref dm);
            dm.ID_ToaDo = Convert.ToInt32(TB_ID_ToaDo.Text);
            dm.ToaDo_X = TB_ToaDoX.Text;
            dm.ToaDo_Y = TB_ToaDoY.Text;
            Cua_Hang.BUS.ToaDo_KhachHang_BUS.Sua_ToaDoCuaHang_BUS(dm);

        }
        protected void Bt_capnhat_Click(object sender, EventArgs e)
        {
            if(KiemTra()==true)
            {
                this.CapNhatToaDoCuaHang();
                this.Bt_them.Visible = true;
                this.List_All_ToaDo();
            }
        }

        protected void GV_ToaDo_PageIndexChanged(object sender, EventArgs e)
        {

        }
        //======================================================================
        //-------------------------------------------------






    }
}